package yxy.cherry.battle.module.affix.skill;

import yxy.cherry.battle.Battle;
import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.module.affix.*;
import yxy.cherry.battle.module.affix.AffixFactory.SkillTypeEnum;
import yxy.cherry.battle.module.affix.Buff.BuffType;
import yxy.cherry.battle.module.record.stream.RecordStreamGroup;
import yxy.cherry.battle.module.record.stream.RecordStream_07_驱散效果;
import yxy.cherry.battle.module.record.stream.RecordStream_BFA;
import yxy.cherry.battle.module.record.stream.RecordStream_SKL;
import yxy.cherry.data.bean.skill;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

/** 驱散 */
@SkillType(Type = SkillTypeEnum.驱散)
public class Affix_10_驱散 extends Skill {

	public Affix_10_驱散(Fighter actor, skill skilldata, int probability, int[] params) {
		super(actor, skilldata, probability, params);
	}

	private int getBuffTypeStr() {return getParam(0);}
	private int getRemoveNumber() { return getParam(1);}

	private RecordStreamGroup steamGroup;

	@Override
	public RecordStreamGroup record() {
		steamGroup = getRecord().stream(new RecordStreamGroup());
		steamGroup.add(new RecordStream_SKL(getActor(), getSkillId()));
		return steamGroup;
	}

	@Override
	public boolean exec(Fighter actor, Fighter target) {
		ArrayList<Integer> arrs = UtilTool.Integer2List(getBuffTypeStr());
		int removeNumber = getRemoveNumber();

		HashSet<BuffType> typeSet = new HashSet<>();
		for (int i = 0; i < arrs.size(); i++) {
			BuffType type = BuffType.forNumber(arrs.get(i));
			if (type.equals(BuffType.无分类)) {
				continue;
			}
			typeSet.add(type);
		}

		RecordStream_07_驱散效果 recordSteam = new RecordStream_07_驱散效果(target);

		ArrayList<Buff> buffs = new ArrayList<>();
		for (Buff buff : target.getBuffMap().values()) {
			if (typeSet.contains(buff.getBuffType())) {
				buffs.add(buff);
			}
		}
		Collections.shuffle(buffs);
		int realRemoveNumber = Math.min(removeNumber,buffs.size());//需要求  移除的异常buff、异常Buff的数量的最小值


		ArrayList<Buff> buffList = new ArrayList<>();
		for(int i = 0;i < realRemoveNumber;i++){
			Buff buff = buffs.get(i);
			target.removeBuff(buff);
			buffList.add(buff);
		}

		if(buffList.size()==0){
			Battle.battleLogger(6,"驱散：施法者："+actor.getName()+"目标："+target.getName()+"驱散失败"+buffList.size());
			return false;
		}

		recordSteam.addBuffs(buffList);
		steamGroup.add(recordSteam).record();
		getRecord().stream(new RecordStream_BFA(target,buffList)).record();

		String buffString = "";
		for(Buff buff1:buffList){
			buffString = buff1.getBuffId()+buff1.getSkilldata().getName()+"|";
		}
		Battle.battleLogger(6,"驱散：施法者："+actor.getName()+"目标："+target.getName()+"驱散的buff"+buffString);
		return true;
	}

}